<?php

namespace app\common\model;

use app\common\model\Order as OrderModel;
use think\Model;
use traits\model\SoftDelete;

class Stat extends Model
{

    //服务小时数
    public function orderHour($staff_id,$start_date,$end_date,$store_id=0)
    {
        //项目小时
        if ($store_id){
            $where = ['o.store_id'=>$store_id,'o.is_end'=>1];
        } else {
            $where = ['o.staff_id'=>$staff_id,'o.is_end'=>1];
        }

        $order_hour = OrderModel::alias('o')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->where($where)
            ->where('o.end_time', 'between time', [$start_date, $end_date])
            ->sum('sp.hour');
        //项目分钟
        $order_minute = OrderModel::alias('o')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->where($where)
            ->where('o.end_time', 'between time', [$start_date, $end_date])
            ->sum('sp.minute');

        //加钟的项目小时
        if ($store_id){
            $subwhere = ['o.staff_id'=>$staff_id,'o.is_end'=>1];
        } else {
            $subwhere = ['o.staff_id'=>$staff_id,'o.is_end'=>1];
        }

        $suborder_hour = Suborder::alias('so')
            ->join('order o','so.order_id = o.order_id')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->where($subwhere)
            ->where('o.end_time', 'between time', [$start_date, $end_date])
            ->sum('sp.hour');
        //加钟的项目分钟
        $suborder_minute = Suborder::alias('so')
            ->join('order o','so.order_id = o.order_id')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->where($subwhere)
            ->where('o.end_time', 'between time', [$start_date, $end_date])
            ->sum('sp.minute');


        $hour = bcadd($order_hour,$suborder_hour,2);

        $total_minute =  bcdiv(bcadd($order_minute,$suborder_minute,2),60,2);

        $total_hour = bcadd($hour,$total_minute,2);
        return $total_hour;
    }

    //钟数
    public function projectNum($staff_id,$start_date='',$end_date='',$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'is_end'=>1];
        } else {
            $where = ['store_id'=>$store_id,'is_end'=>1];
        }
        if ($start_date&&$end_date){
            $where['end_time'] = ['between', [$start_date, $end_date]];
        }

        $project_num = OrderModel::where($where)
            ->sum('num');

        //加钟的项目数量

        if ($staff_id){
            $sub_where = ['so.staff_id'=>$staff_id,'o.is_end'=>1];
        } else {
            $sub_where = ['o.store_id'=>$store_id,'o.is_end'=>1];
        }
        if ($start_date&&$end_date){
            $sub_where['o.end_time'] = ['between', [$start_date, $end_date]];
        }

        $suborder_num = Suborder::alias('so')
            ->join('order o','so.order_id = o.order_id')
            ->where($sub_where)
            ->sum('so.num');

        return $project_num+$suborder_num;
    }

    //刷卡金额
    public function swipingCardPrice($staff_id,$start_date,$end_date,$store_id=0)
    {
        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }


        if ($start_date&&$end_date){
            $sub_where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $swiping_card_price = OrderModel::where($where)->sum('swiping_card_price');

        return $swiping_card_price;
    }

    //刷卡小费
    public function swipingCardTipPrice($staff_id,$start_date,$end_date,$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }

        if ($start_date&&$end_date){
            $where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $swiping_card_tip_price = OrderModel::where($where)->sum('swiping_card_tip_price');

        return $swiping_card_tip_price;
    }

    //现金
    public function cashPrice($staff_id,$start_date,$end_date,$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }

        if ($start_date&&$end_date){
            $where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $cash_price = OrderModel::where($where)->sum('cash_price');

        return $cash_price;
    }

    //礼品卡
    public function giftCardPrice($staff_id,$start_date,$end_date,$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }

        if ($start_date&&$end_date){
            $where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $gift_card_price = OrderModel::where($where)->sum('gift_card_price');

        return $gift_card_price;
    }

    //礼品卡小费
    public function giftCardTipPrice($staff_id,$start_date,$end_date,$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }

        if ($start_date&&$end_date){
            $where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $gift_card_tip_price = OrderModel::where($where)->sum('gift_card_tip_price');

        return $gift_card_tip_price;
    }


    //计算付款金额(刷卡金额+刷卡小费+现金+礼品卡+礼品卡小费)
    public function orderPayPrice($staff_id,$start_date,$end_date,$store_id=0)
    {

        if ($staff_id){
            $where = ['staff_id'=>$staff_id,'pay_status'=>1];
        } else {
            $where = ['store_id'=>$store_id,'pay_status'=>1];
        }

        if ($start_date&&$end_date){
            $where['pay_time'] = ['between', [$start_date, $end_date]];
        }

        $gift_card_price = OrderModel::where($where)->sum('gift_card_price');

        $gift_card_tip_price = OrderModel::where($where)->sum('gift_card_tip_price');

        $cash_price = OrderModel::where($where)->sum('cash_price');

        $swiping_card_price = OrderModel::where($where)->sum('swiping_card_price');

        $swiping_card_tip_price = OrderModel::where($where)->sum('swiping_card_tip_price');

        $pay_price = $gift_card_price+$gift_card_tip_price+$cash_price+$swiping_card_price+$swiping_card_tip_price;

        return $pay_price;
    }
}